<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>模块 (C++20 起)</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/modules","wgTitle":"cpp/language/modules","wgCurRevisionId":73255,"wgArticleId":12030,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/modules","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_modules skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">模块 <span class="t-mark-rev t-since-cxx20">(C++20 起)</span></h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../container.html" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="basic_concepts.html" title="cpp/language/basic concepts">基本概念</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../keywords.html" title="cpp/keyword"> 语言关键词</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="charset.html" title="cpp/language/charset"> 字符集与编码</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="translation_phases.html" title="cpp/language/translation phases"> 翻译阶段</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../comments.html" title="cpp/comment"> 注释</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="main_function.html" title="cpp/language/main function"> <code>main()</code> 函数</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="punctuators.html" title="cpp/language/punctuators"> 标点</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="name.html" title="cpp/language/identifiers"> 名字和标识符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="incomplete_type.html" title="cpp/language/type"> 类型</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="types.html" title="cpp/language/types"> 基础类型</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="object.html" title="cpp/language/object"> 对象</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="scope.html" title="cpp/language/scope"> 作用域</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lifetime.html" title="cpp/language/lifetime"> 对象生存期</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="cpp/language/storage duration"> 存储期与连接</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="definition.html" title="cpp/language/definition"> 定义和单一定义规则（ODR）</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lookup.html" title="cpp/language/lookup"> 名字查找</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="qualified_lookup.html" title="cpp/language/qualified lookup"> 有限定名字查找</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="unqualified_lookup.html" title="cpp/language/unqualified lookup"> 无限定名字查找</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="as_if.html" title="cpp/language/as if"> “如同”规则</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="ub.html" title="cpp/language/ub"> 未定义行为</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="memory_model.html" title="cpp/language/memory model"> 内存模型</a> </td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>大多数 C++ 项目用到了多个翻译单元，因此它们需要在那些单元间分享<a href="declarations.html" title="cpp/language/declarations">声明</a>和<a href="definition.html" title="cpp/language/definition">定义</a>。正是因为这样，使用头文件非常重要，例如标准库的声明通过 <a href="../preprocessor/include.html" title="cpp/preprocessor/include"><tt>
#include</tt></a> 对应的头提供。
</p><p>模块是一个用于在翻译单元间分享声明和定义的语言特性。它们可以在某些地方替代使用头文件。
</p><p>模块和<a href="namespace.html" title="cpp/language/namespace">命名空间</a>是正交的。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">// helloworld.cpp</span>
export module helloworld<span class="sy4">;</span>  <span class="co1">// 模块声明</span>
import <span class="sy1">&lt;</span>iostream<span class="sy1">&gt;</span><span class="sy4">;</span>         <span class="co1">// 导入声明</span>
 
export <span class="kw4">void</span> hello<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>      <span class="co1">// 导出声明</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"Hello world!<span class="es1">\n</span>"</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">// main.cpp</span>
import helloworld<span class="sy4">;</span>  <span class="co1">// 导入声明</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    hello<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E8.AF.AD.E6.B3.95"> 语法 </span></h3>
<table class="t-sdsc-begin">


<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>export</b></code><span class="t-mark">(可选)</span> <code><b>module</b></code> <span class="t-spar">模块名</span> <span class="t-spar">模块分区</span><span class="t-mark">(可选)</span> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>;</b></code>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>




<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>export</b></code> <span class="t-spar">声明</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>




<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>export</b></code> <code><b>{</b></code> <span class="t-spar">声明序列</span><span class="t-mark">(可选)</span> <code><b>} </b></code>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>




<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>export</b></code><span class="t-mark">(可选)</span> <code><b>import</b></code> <span class="t-spar">模块名</span> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>;</b></code>
</td>
<td> (4)
</td>
<td class="t-sdsc-nopad">
</td></tr>




<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>export</b></code><span class="t-mark">(可选)</span> <code><b>import</b></code> <span class="t-spar">模块分区</span> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>;</b></code>
</td>
<td> (5)
</td>
<td class="t-sdsc-nopad">
</td></tr>




<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>export</b></code><span class="t-mark">(可选)</span> <code><b>import</b></code> <span class="t-spar">头名</span> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>;</b></code>
</td>
<td> (6)
</td>
<td class="t-sdsc-nopad">
</td></tr>




<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>module</b></code> <code><b>;</b></code>
</td>
<td> (7)
</td>
<td class="t-sdsc-nopad">
</td></tr>




<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>module : private </b></code> <code><b>;</b></code>
</td>
<td> (8)
</td>
<td class="t-sdsc-nopad">
</td></tr>




<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 模块声明。声明当前翻译单元是一个<i>模块单元</i>。</div>
<div class="t-li1"><span class="t-li">2,3)</span> 导出声明。导出 <span class="t-spar">声明</span> 或 <span class="t-spar">声明序列</span> 中所有具有命名空间作用域的声明。</div>
<div class="t-li1"><span class="t-li">4,5,6)</span> 导入声明。导入一个模块单元，模块分区，或头单元。</div>
<div class="t-li1"><span class="t-li">7)</span> 开始一个<i>全局模块片段</i>。</div>
<div class="t-li1"><span class="t-li">8)</span> 开始一个<i>私有模块片段</i>。</div>
<h3><span class="mw-headline" id=".E6.A8.A1.E5.9D.97.E5.A3.B0.E6.98.8E"> 模块声明 </span></h3>
<p>翻译单元可以有一个模块声明，这种情况下它们被视为<i>模块单元</i>。<i>模块声明</i>在有提供时必须是翻译单元的首个声明（后面提到的<i>全局模块片段</i>除外）。每个模块单元都对应一个<i>模块名</i>（可以带一个分区），它在模块声明中提供。
</p>
<table>
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <code><b>export</b></code><span class="t-mark">(可选)</span> <code><b>module</b></code> <span class="t-spar">模块名</span> <span class="t-spar">模块分区</span><span class="t-mark">(可选)</span> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>;</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>


<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>模块名包含由点分隔的一个或多个标识符（例如：<code>mymodule</code>，<code>mymodule.mysubmodule</code>，<code>mymodule2</code>...）。点没有内在含义，不过它们会非正式地用于表示继承关系。
</p><p>一个<i>具名模块</i>是一组模块名相同的模块单元。
</p><p>声明中带有关键词 <code><b>export</b></code> 的模块单元是<i>模块接口单元</i>。其他模块单元被称为<i>模块实现单元</i>。
</p><p>对于每个具名模块，必须有恰好一个未指定模块分区的模块接口单元。这个模块单元被称为<i>主模块接口单元</i>。在导入对应的具名模块时可以使用它导出的内容。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">// （每行表示一个单独的翻译单元）</span>
 
export module A<span class="sy4">;</span>   <span class="co1">// 为具名模块 'A' 声明主模块接口单元</span>
module A<span class="sy4">;</span>          <span class="co1">// 为具名模块 'A' 声明一个模块实现单元</span>
module A<span class="sy4">;</span>          <span class="co1">// 为具名模块 'A' 声明另一个模块实现单元</span>
export module A.<span class="me1">B</span><span class="sy4">;</span> <span class="co1">// 为具名模块 'A.B' 声明主模块接口单元</span>
module A.<span class="me1">B</span><span class="sy4">;</span>        <span class="co1">// 为具名模块 'A.B' 声明一个模块实现单元</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.AF.BC.E5.87.BA.E5.A3.B0.E6.98.8E.E5.92.8C.E5.AE.9A.E4.B9.89"> 导出声明和定义 </span></h3>
<p>模块接口单元可以导出声明和定义，这些内容可以导入到其他翻译单元。它们可以有 <code><b>export</b></code> 关键词作为前缀，或者处于 <code><b>export</b></code> 块中。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <code><b>export</b></code> <span class="t-spar">声明</span>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <code><b>export</b></code> <code><b>{</b></code> <span class="t-spar">声明序列</span><span class="t-mark">(可选)</span> <code><b>} </b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1">export module A<span class="sy4">;</span>   <span class="co1">// 为具名模块 'A' 声明主模块接口单元</span>
 
<span class="co1">// hello() 会在所有导入 'A' 的翻译单元中可见</span>
export <span class="kw4">char</span> <span class="kw4">const</span><span class="sy2">*</span> hello<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="st0">"hello"</span><span class="sy4">;</span> <span class="br0">}</span> 
 
<span class="co1">// world() 不可见</span>
<span class="kw4">char</span> <span class="kw4">const</span><span class="sy2">*</span> world<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="st0">"world"</span><span class="sy4">;</span> <span class="br0">}</span>
 
<span class="co1">// one() 和 zero() 均可见</span>
export <span class="br0">{</span>
    <span class="kw4">int</span> one<span class="br0">(</span><span class="br0">)</span>  <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="br0">}</span>
    <span class="kw4">int</span> zero<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">0</span><span class="sy4">;</span> <span class="br0">}</span>
<span class="br0">}</span>
 
<span class="co1">// 也可以导出命名空间：hi::english() 和 hi::french() 均可见</span>
export <span class="kw1">namespace</span> hi <span class="br0">{</span>
    <span class="kw4">char</span> <span class="kw4">const</span><span class="sy2">*</span> english<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="st0">"Hi!"</span><span class="sy4">;</span> <span class="br0">}</span>
    <span class="kw4">char</span> <span class="kw4">const</span><span class="sy2">*</span> french<span class="br0">(</span><span class="br0">)</span>  <span class="br0">{</span> <span class="kw1">return</span> <span class="st0">"Salut!"</span><span class="sy4">;</span> <span class="br0">}</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.AF.BC.E5.85.A5.E6.A8.A1.E5.9D.97.E5.92.8C.E5.A4.B4"> 导入模块和头 </span></h3>
<p>模块可以通过<i>导入声明</i>导入：
</p>
<table class="t-sdsc-begin">
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <code><b>export</b></code><span class="t-mark">(可选)</span> <code><b>import</b></code> <span class="t-spar">模块名</span> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>;</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>


<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>所有在给定具名模块的模块接口单元中导出的声明和定义都会在使用导入声明的翻译单元中可见。
</p><p>导入的声明在模块接口单元里可以再导出。也就是说，如果模块 A 导入 B 后又导出，那么导入 A 也会使所有从 B 导出的内容可见。
</p><p>在模块单元里，所有导入声明（包括带导出的导入）必须集中在模块声明后以及所有其他声明前。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">/////// A.cpp    （'A' 的主模块接口单元）</span>
export module A<span class="sy4">;</span>
 
export <span class="kw4">char</span> <span class="kw4">const</span><span class="sy2">*</span> hello<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="st0">"hello"</span><span class="sy4">;</span> <span class="br0">}</span>
 
<span class="co1">/////// B.cpp    （'B' 的主模块接口单元）</span>
export module B<span class="sy4">;</span>
 
export import A<span class="sy4">;</span>
 
export <span class="kw4">char</span> <span class="kw4">const</span><span class="sy2">*</span> world<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="st0">"world"</span><span class="sy4">;</span> <span class="br0">}</span>
 
<span class="co1">/////// main.cpp （非模块单元）</span>
<span class="co2">#include &lt;iostream&gt;</span>
import B<span class="sy4">;</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> hello<span class="br0">(</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> world<span class="br0">(</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>在模块单元里（<i>全局模块片段</i>以外）不能使用 <a href="../preprocessor/include.html" title="cpp/preprocessor/include"><tt>
#include</tt></a>，因为所有被包含的声明和定义都会作为模块的一部分。头可以改为通过<i>导入声明</i>导入：
</p>
<table class="t-sdsc-begin">
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <code><b>export</b></code><span class="t-mark">(可选)</span> <code><b>import</b></code> <span class="t-spar">头名</span> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>;</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>


<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>导入一个头文件会使其所有声明和定义可见。预处理宏也会可见（因为预处理器会识别导入声明）。然而与 <code>#include</code> 相反的是，在（导入这个头文件的）翻译单元中定义的预处理宏不会影响头文件的处理。这在某些场合会不方便（某些头文件用预处理宏作为配置方式），这种情况下需要使用<i>全局模块片段</i>。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">/////// A.cpp    （'A' 的主模块接口单元）</span>
export module A<span class="sy4">;</span>
 
import <span class="sy1">&lt;</span>iostream<span class="sy1">&gt;</span><span class="sy4">;</span>
export import <span class="sy1">&lt;</span>string_view<span class="sy1">&gt;</span><span class="sy4">;</span>
 
export <span class="kw4">void</span> print<span class="br0">(</span><a href="../string/basic_string_view.html"><span class="kw1248">std::<span class="me2">string_view</span></span></a> message<span class="br0">)</span> <span class="br0">{</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> message <span class="sy1">&lt;&lt;</span> <a href="../io/manip/endl.html"><span class="kw1791">std::<span class="me2">endl</span></span></a><span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="co1">/////// main.cpp （非模块单元）</span>
import A<span class="sy4">;</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <a href="../string/basic_string_view.html"><span class="kw1248">std::<span class="me2">string_view</span></span></a> message <span class="sy1">=</span> <span class="st0">"Hello, world!"</span><span class="sy4">;</span>
    print<span class="br0">(</span>message<span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.85.A8.E5.B1.80.E6.A8.A1.E5.9D.97.E7.89.87.E6.AE.B5"> 全局模块片段 </span></h3>
<p>模块单元可以有<i>全局模块片段</i>前缀，它可以在无法导入头文件时（尤其是在头文件用预处理宏进行配置时）包含它们。
</p>
<table class="t-sdsc-begin">
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <code><b>module;</b></code>
<p><span class="t-spar">预处理指令序列</span><span class="t-mark">(可选)</span>
</p><p><span class="t-spar">模块声明</span>
</p>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>


<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>如果一个模块单元有一个全局模块片段，那么它的首个声明必须是 <code><b>module;</b></code>。然后在全局模块片段中只能出现<a href="../preprocessor.html#.E6.8C.87.E4.BB.A4" title="cpp/preprocessor">预处理指令</a>。然后用一个标准的模块声明结束这个全局模块片段，后面就是模块内容。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">/////// A.cpp    （'A' 的主模块接口单元）</span>
module<span class="sy4">;</span>
 
<span class="co1">// 按照 POSIX 标准，定义 _POSIX_C_SOURCE 会向标准头中添加函数。</span>
<span class="co2">#define _POSIX_C_SOURCE 200809L</span>
<span class="co2">#include &lt;stdlib.h&gt;</span>
 
export module A<span class="sy4">;</span>
 
import <span class="sy1">&lt;</span>ctime<span class="sy1">&gt;</span><span class="sy4">;</span>
 
<span class="co1">// 仅用于演示（差的随机源）。应改为使用 C++ &lt;random&gt;。</span>
export <span class="kw4">double</span> weak_random<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <a href="../chrono/c/timespec.html"><span class="kw1026">std::<span class="me2">timespec</span></span></a> ts<span class="sy4">;</span>
    <a href="../chrono/c/timespec_get.html"><span class="kw1013">std::<span class="me2">timespec_get</span></span></a><span class="br0">(</span><span class="sy3">&amp;</span>ts, <a href="../chrono/c/timespec_get.html"><span class="kw1014">TIME_UTC</span></a><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 来自 &lt;ctime&gt;</span>
    <span class="co1">// 按照 POSIX 标准从 &lt;stdlib.h&gt; 提供。</span>
    srand48<span class="br0">(</span>ts.<span class="me1">tv_nsec</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="co1">// drand48() 返回 0 与 1 之间的一个随机数</span>
    <span class="kw1">return</span> drand48<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="co1">/////// main.cpp （非模块单元）</span>
import <span class="sy1">&lt;</span>iostream<span class="sy1">&gt;</span><span class="sy4">;</span>
import A<span class="sy4">;</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"0 与 1 之间的随机值："</span> <span class="sy1">&lt;&lt;</span> weak_random<span class="br0">(</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E6.A8.A1.E5.9D.97.E5.88.86.E5.8C.BA"> 模块分区 </span></h3>
<p>一个模块可以有<i>模块分区单元</i>。它们是模块声明中包含了一个模块分区的模块单元，模块分区在模块名之后，以一个冒号 <code><b>:</b></code> 开头。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1">export module A<span class="sy4">:</span>B<span class="sy4">;</span> <span class="co1">// 为模块 'A' 分区 'B' 声明一个模块接口单元。</span></pre></div></div>
<p>一个模块分区表示恰好一个模块单元（两个模块单元不能指定同一个模块分区）。它们只在自己所在的具名模块内部可见（在该具名模块外的翻译单元不能直接导入这些模块分区）。
</p><p>模块分区只能被相同具名模块的模块单元导入。
</p>
<table class="t-sdsc-begin">
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <code><b>export</b></code><span class="t-mark">(可选)</span> <code><b>import</b></code> <span class="t-spar">模块分区</span> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>;</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>


<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">/////// A-B.cpp   </span>
export module A<span class="sy4">:</span>B<span class="sy4">;</span>
...
 
<span class="co1">/////// A-C.cpp</span>
module A<span class="sy4">:</span>C<span class="sy4">;</span>
...
 
<span class="co1">/////// A.cpp</span>
export module A<span class="sy4">;</span>
 
import <span class="sy4">:</span>C<span class="sy4">;</span>
export import <span class="sy4">:</span>B<span class="sy4">;</span>
 
...</pre></div></div>
<p>模块分区内的所有声明和定义在将其导入的模块单元中均可见，无论它们是否被导出。
</p><p>模块分区可以是模块接口单元（如果模块声明中有 <code><b>export</b></code>）。它们必须被主模块接口单元在导入同时导出，并且它们导出的语句在模块被导入时均可见。
</p>
<table class="t-sdsc-begin">
<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <code><b>export</b></code><span class="t-mark">(可选)</span> <code><b>import</b></code> <span class="t-spar">模块分区</span> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <code><b>;</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>


<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">///////  A.cpp   </span>
export module A<span class="sy4">;</span>   <span class="co1">// 主模块接口单元</span>
 
export import <span class="sy4">:</span>B<span class="sy4">;</span>     <span class="co1">// Hello() 在导入 'A' 时可见</span>
import <span class="sy4">:</span>C<span class="sy4">;</span>            <span class="co1">// 现在 WorldImpl() 只对 'A.cpp' 可见</span>
<span class="co1">// export import :C;  // 错误：无法导出模块实现单元</span>
 
<span class="co1">// World() 对所有导入 'A' 的翻译单元均可见</span>
export <span class="kw4">char</span> <span class="kw4">const</span><span class="sy2">*</span> World<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="kw1">return</span> WorldImpl<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">/////// A-B.cpp </span>
export module A<span class="sy4">:</span>B<span class="sy4">;</span> <span class="co1">// 模块分区接口单元</span>
 
<span class="co1">// Hello() 对所有导入 'A' 的翻译单元均可见</span>
export <span class="kw4">char</span> <span class="kw4">const</span><span class="sy2">*</span> Hello<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="st0">"Hello"</span><span class="sy4">;</span> <span class="br0">}</span></pre></div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">/////// A-C.cpp </span>
module A<span class="sy4">:</span>C<span class="sy4">;</span>        <span class="co1">// 模块分区实现单元</span>
 
<span class="co1">// WorldImpl() 对 'A' 中所有导入 ':C' 的翻译单元均可见</span>
<span class="kw4">char</span> <span class="kw4">const</span><span class="sy2">*</span> WorldImpl<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="st0">"World"</span><span class="sy4">;</span> <span class="br0">}</span></pre></div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">/////// main.cpp </span>
import A<span class="sy4">;</span>
import <span class="sy1">&lt;</span>iostream<span class="sy1">&gt;</span><span class="sy4">;</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> Hello<span class="br0">(</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> World<span class="br0">(</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
    <span class="co1">// WorldImpl(); // 错误：WorldImpl() 不可见</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.85.B3.E9.94.AE.E8.AF.8D"> 关键词 </span></h3>
<p><a href="../keyword/export.html" title="cpp/keyword/export"><tt>export</tt></a>，<a href="../keyword/import.html" title="cpp/keyword/import"><tt>import</tt></a>，<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/keyword/module&amp;action=edit&amp;redlink=1" class="new" title="cpp/keyword/module（页面不存在）"><tt>module</tt></a>
</p>
<!-- 
NewPP limit report
Preprocessor visited node count: 2068/1000000
Preprocessor generated node count: 5129/1000000
Post‐expand include size: 46924/2097152 bytes
Template argument size: 13430/2097152 bytes
Highest expansion depth: 13/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:12030-0!*!0!!zh!*!zh!* and timestamp 20211219085644 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/modules&amp;oldid=73255">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/modules&amp;oldid=73255</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/modules">Online version</a></li><li>Offline version retrieved 2022-01-01 00:37.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2021年12月18日 (星期六) 20:48。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过9,077次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.099 secs. -->
	</body>
<!-- Cached 20211219193147 -->
</html>